package com.hs.wdss.action.data;
 
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.hs.core.action.BaseAction;
import com.hs.core.db.DBMgr;
import com.hs.core.utils.PageParam;
import com.hs.core.utils.PageUtil;
import com.hs.core.utils.SystemUtil;
import com.hs.wdss.model.WdssDisinfo;
import com.hs.wdss.model.WdssResource;
import com.hs.wdss.service.IWdssService;
@Controller  
public class DataController  extends BaseAction {
    /**
	 * 
	 */
	private static final long serialVersionUID = -354434172898440670L;
	private static final Logger logger = Logger.getLogger(DataController.class);
	private IWdssService wdssService;
	@Autowired  
	public void setRoleService(IWdssService wdssService) {
		this.wdssService = wdssService;
	}
	 /**
	  * 数据管理主页
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/data/dataListFrame.do", method = RequestMethod.GET)   
     public ModelAndView dataListFrame(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		   	
		  return new ModelAndView("app/data/datalistframe",modelMap);
		 
	 }
	 /**
	  * 期刊数据管理主页
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/data/datamagaListFrame.do", method = RequestMethod.GET)   
     public ModelAndView datamagaListFrame(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		   	
		  return new ModelAndView("app/data/datamagalistframe",modelMap);
		 
	 }
	 /**
	  * 数据详细信息
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/data/magainfo.do")   
     public ModelAndView magainfo(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 String pid = request.getParameter("id");
		 System.out.println("题录信息主键：" + pid);
		 //题录信息
		 WdssResource maga = (WdssResource)this.wdssService.findByID(WdssResource.class, new Long(pid));
		 modelMap.put("obj", maga);
		 return new ModelAndView("app/data/magainfo",modelMap);
		 
	 }
	 /**
	  * 数据管理列表
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/data/dataList.do")   
     public ModelAndView dataList(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 PageParam pageParam = new  PageParam(request,"data/dataList.do");
		 PageUtil pageUtil = null;
		 
		 StringBuffer searchBuffer = new StringBuffer(" select a from WdssResource a where dstype=1 ");
			String bookname = request.getParameter("bookname");  
			String publishunit = request.getParameter("publishunit"); 
			request.setAttribute("bookname", bookname);
			request.setAttribute("publishunit", publishunit);
			if(StringUtils.isNotBlank(bookname)){
				searchBuffer.append(" and (a.bookname like '%").append(bookname).append("%') ");
				pageParam.getConditionMap().put("bookname", bookname);
				request.setAttribute("bookname", bookname);
			}  
			if(StringUtils.isNotBlank(publishunit)){
				pageParam.getConditionMap().put("publishunit", publishunit);
				searchBuffer.append(" and (a.publishunit like '%"+publishunit+"%') "); 
			}
			Map data = new HashMap();
			pageParam.setSearchSql(searchBuffer.toString());
			pageUtil = wdssService.getList(pageParam);
 			data.put("dataList", pageUtil);     
		 return new ModelAndView("app/data/dataList",data);
		 
	 } 
	 /**
	  * 期刊数据管理列表
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/data/datamagaList.do")   
     public ModelAndView datamagaList(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 PageParam pageParam = new  PageParam(request,"data/datamagaList.do");
		 PageUtil pageUtil = null;
		 
		 StringBuffer searchBuffer = new StringBuffer(" select a from WdssResource a where dstype=2 ");
			String maganame = request.getParameter("maganame"); 
			String title = request.getParameter("title");  

 			request.setAttribute("maganame", maganame);
			request.setAttribute("title", title);
			if(StringUtils.isNotBlank(maganame)){
				searchBuffer.append(" and (a.maganame like '%").append(maganame).append("%') ");
				pageParam.getConditionMap().put("maganame", maganame);
				request.setAttribute("maganame", maganame);
			}  
			if(StringUtils.isNotBlank(title)){
				pageParam.getConditionMap().put("title", title);
				searchBuffer.append(" and (a.title like '%"+title+"%') "); 
			}
			Map data = new HashMap();
			pageParam.setSearchSql(searchBuffer.toString());
			pageUtil = wdssService.getList(pageParam);
 			data.put("dataList", pageUtil);     
		 return new ModelAndView("app/data/datamagaList",data);
		 
	 } 
	 /**
	  * 数据导入页面
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/data/dataDistribute.do", method = RequestMethod.GET)   
     public ModelAndView dataDistribute(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		
		 return new ModelAndView("app/data/datadistribute",modelMap);
	 } 
	 /**
	  * 期刊数据导入页面
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/data/datamagaDistribute.do", method = RequestMethod.GET)   
     public ModelAndView datamagaDistribute(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		
		 return new ModelAndView("app/data/datamagadistribute",modelMap);
	 } 
	 /**
	  * 期刊数据导入
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	 * @throws Exception 
	  */
	 @RequestMapping(value="/data/importmagadata.do")   
     public ModelAndView importmagadata(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws Exception {
		 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;   
		 MultipartFile multipartFile = multipartRequest.getFile("importfile");   
		 String fileName = multipartFile.getOriginalFilename(); 
		 String webRoot = SystemUtil.getWebappRoot().toString();
		 webRoot = webRoot.replace("\\", "/") + "/temp";
		 System.out.println("webRoot::" + webRoot );
		 String sep = System.getProperty("file.separator");
		 sep = "/"; 
		 File rootDir = new File(webRoot); 
		 if (!rootDir.exists()) {
			rootDir.mkdirs(); 
		 }
		 File file = new File(webRoot + sep + fileName);
		 BufferedOutputStream bf = new BufferedOutputStream(new FileOutputStream(file));
		 bf.write(multipartFile.getBytes());
		 bf.close();
		 System.out.println("上传成功。。。。。。");
		 this.readmagaXls(file);
	     response.getWriter().write("1");
		 return new ModelAndView("app/data/datamagaList",modelMap);
	 }
	 /**
	  * 数据导入
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	 * @throws Exception 
	  */
	 @RequestMapping(value="/data/importdata.do")   
     public ModelAndView importXlsData(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws Exception {
		 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;   
		 MultipartFile multipartFile = multipartRequest.getFile("importfile");   
		 String fileName = multipartFile.getOriginalFilename(); 
		 String webRoot = SystemUtil.getWebappRoot().toString();
		 webRoot = webRoot.replace("\\", "/") + "/temp";
		 System.out.println("webRoot::" + webRoot );
		 String sep = System.getProperty("file.separator");
		 sep = "/"; 
		 File rootDir = new File(webRoot); 
		 if (!rootDir.exists()) {
			rootDir.mkdirs(); 
		 }
		 File file = new File(webRoot + sep + fileName);
		 BufferedOutputStream bf = new BufferedOutputStream(new FileOutputStream(file));
		 bf.write(multipartFile.getBytes());
		 bf.close();
		 System.out.println("上传成功。。。。。。");
		 this.readXls(file);
	     response.getWriter().write("1");
		 return new ModelAndView("app/data/dataList",modelMap);
	 }
	 /**
	  * 数据修改
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/data/saveData.do", method = RequestMethod.GET)   
     public ModelAndView saveData(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
         
		 return new ModelAndView("app/data/dataList",modelMap);
		 
	 } 
	 /**
	  * 数据删除
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/data/dataDelete.do", method = RequestMethod.GET)   
     public ModelAndView dataDelete(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
         
		 return new ModelAndView("app/data/dataList",modelMap);
		 
	 } 
	/**
	  * 读取excel文件
	  * @param file 要读取的excel文件
	  */
	 public void readXls(File file){
			HSSFWorkbook workbook = null;
			try {
				FileInputStream rs = new FileInputStream(file) ;      
				workbook = new HSSFWorkbook(rs);
				int total = workbook.getNumberOfSheets(); //获取sheet个数      
		        int start = 0;
		        int end = 0 ;  
		        String discode = "";
				for(int i = 0 ; i < total ; i ++){  
		            HSSFSheet sheet = workbook.getSheetAt(i);// 按索引获取sheet引用          
		            System.out.println(sheet.getSheetName());      
		            start = sheet.getFirstRowNum();  //sheet起始行索引      
		            end = sheet.getLastRowNum();//sheet起结束行索引      
		            int temp = 1;      
		            HSSFRow row = null ;      
		            HSSFCell bookname = null ;      
		            HSSFCell author = null ; 
		            HSSFCell publishunit = null ;
		            HSSFCell publishaddress = null ;
		            HSSFCell publishdate = null ;
		            HSSFCell isbnstr = null ;
		            HSSFCell banci = null ;
		            HSSFCell yinci = null ;
		            HSSFCell zishu = null ; 
		            HSSFCell subjectword = null ;
		            HSSFCell keyword = null ; 
		            HSSFCell abstract_ = null ; 
		            HSSFCell mrcode_ = null ; 
		            
		            while(temp <= end){      
		            	 WdssResource maga = new WdssResource();
		                 row =  sheet.getRow(temp);//获取第N行表格数据     
		                 if(row != null){
		                	 temp ++ ;
		                	  bookname = row.getCell(1);//获取指定列单元格            
		                	  author = row.getCell(2);   
		                	  publishunit = row.getCell(3);
		                	  publishaddress = row.getCell(4);
		                	  publishdate = row.getCell(5);
		                	  isbnstr = row.getCell(6);
		                	  banci = row.getCell(7);
		                	  yinci = row.getCell(8);
		                	  zishu = row.getCell(9);
		                	  subjectword = row.getCell(10);
		                	  keyword = row.getCell(11);
		                	  abstract_ = row.getCell(12);
		                	  mrcode_ = row.getCell(13);
				                 if(null != bookname){
				                	 maga.setBookname(getCell(bookname)); 
				                 }else{
				                	 maga.setBookname("");
				                 }
				                 if(null != author){
				                	 maga.setAuthor(getCell(author)); 
				                 }else{
				                	 maga.setAuthor("");
				                 }
				                 if(null != publishunit){
				                	 maga.setPublishunit(getCell(publishunit)); 
				                 }else{
				                	 maga.setPublishunit("");
				                 }
				                 if(null != publishaddress){
				                	 maga.setPublishaddress(getCell(publishaddress)); 
				                 }else{
				                	 maga.setPublishaddress("");
				                 }
				                 if(null != publishdate){
				                	 maga.setPublishdate(getCell(publishdate)); 
				                 }else{
				                	 maga.setPublishdate("");
				                 }
				                 if(null != isbnstr){
				                	 maga.setIsbnstr(getCell(isbnstr)); 
				                 }else{
				                	 maga.setIsbnstr("");
				                 }
				                 if(null != banci){
				                	 maga.setBanci(getCell(banci)); 
				                 }else{
				                	 maga.setBanci("");
				                 }
				                 if(null != yinci){
				                	 maga.setYinci(getCell(yinci)); 
				                 }else{
				                	 maga.setYinci("");
				                 }
				                 if(null != zishu){
				                	 maga.setZishu(getCell(zishu)); 
				                 }else{
				                	 maga.setZishu("");
				                 }
				                 if(null != subjectword){
				                	 maga.setSubjectword(getCell(subjectword)); 
				                 }else{
				                	 maga.setSubjectword("");
				                 }
				                 if(null != abstract_){
				                	 maga.setAbstract_(getCell(abstract_)); 
				                 }else{
				                	 maga.setAbstract_("");
				                 }
				                 if(null != keyword){
				                	 maga.setKeyword(getCell(keyword)); 
				                 }else{
				                	 maga.setKeyword("");
				                 }
				                 if(null != mrcode_ && StringUtils.isNotEmpty(getCell(mrcode_))){
				                	discode = getCell(mrcode_); 
				                 }else{
				                	 //没有医案编号的记录  不做导入，略过
				                	 
				                	 continue;
				                 }
				                 
				                 StringBuffer countsql = new StringBuffer();
				                 countsql.append("select id from wdss_resource ");
				                 countsql.append(" where bookname='");
				                 countsql.append(maga.getBookname());
				                 countsql.append("' and author='");
				                 countsql.append(maga.getAuthor() + "'");
				                 String isbn = maga.getIsbnstr();
				                 if(null != isbn && !"".equals(isbn) && !"null".equals(isbn)){
				                	 countsql.append(" and isbnstr='" + isbn + "'");
				                 }
								 List accList = DBMgr.getDbUtilInst().queryList(countsql.toString(),null);  
								 int acclen = accList.size();
							     if(acclen == 0){
							    	 maga.setDstype(new Long(1));
							    	 wdssService.saveObject(maga);
							    	 WdssDisinfo disinfo = new WdssDisinfo();
							    	 disinfo.setDiscode(discode);
							    	 disinfo.setDsid(maga.getId());
							    	 disinfo.setState("0");
							    	 wdssService.saveObject(disinfo);
							     }else {
							    	 Map data = (Map)accList.get(0);
								     String id = data.get("id") + "";
//								     WdssResource obj = (WdssResource)wdssService.findByID(WdssResource.class, new Long(id));
								     
							    	 StringBuffer usql = new StringBuffer("update wdss_resource set BOOKNAME=?,AUTHOR=?,SUBJECTWORD=?,KEYWORD=?,ABSTRACT=?,PUBLISHUNIT=?,");
							    	 usql.append("PUBLISHADDRESS=?,PUBLISHDATE=?,ISBNSTR=?,BANCI=?,YINCI=?,ZISHU=?");
							    	 usql.append(" where id=?");
							    	 Object[] obj = new Object[]{maga.getBookname(),maga.getAuthor(),maga.getSubjectword(),maga.getKeyword(),maga.getAbstract_(),maga.getPublishunit(),
							    			 maga.getPublishaddress(),maga.getPublishdate(),maga.getIsbnstr(),maga.getBanci(),maga.getYinci(),maga.getZishu(),id};
							    	 DBMgr.getDbUtilInst().update(usql.toString(), obj);
							     }
		                 }
		                  
		            } 
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
	}
	 /**
	  * 读取excel文件
	  * @param file 要读取的excel文件
	  */
	 public void readmagaXls(File file){
			HSSFWorkbook workbook = null;
			try {
				FileInputStream rs = new FileInputStream(file) ;      
				workbook = new HSSFWorkbook(rs);
				int total = workbook.getNumberOfSheets(); //获取sheet个数      
		        int start = 0;
		        int end = 0 ;  
		        String discode = "";
				for(int i = 0 ; i < total ; i ++){  
		            HSSFSheet sheet = workbook.getSheetAt(i);// 按索引获取sheet引用          
 		            start = sheet.getFirstRowNum();  //sheet起始行索引      
		            end = sheet.getLastRowNum();//sheet起结束行索引      
		            int temp = 1;      
		            HSSFRow row = null ;      
		            HSSFCell maganame = null ;      
		            HSSFCell author = null ; 
		            HSSFCell title = null ;
		            HSSFCell titleeng = null ;
		            HSSFCell pulishaddress = null ;
		            HSSFCell publishdate = null ;
		            HSSFCell periodstr = null ;
		            HSSFCell pagenum = null ;
		            HSSFCell yearstr = null ; 
		            HSSFCell subjectword = null ;
		            HSSFCell keyword = null ; 
		            HSSFCell abstract_ = null ; 
		            HSSFCell mrcode_ = null ; 
		            
		            while(temp <= end){      
		            	 WdssResource maga = new WdssResource();
		                 row =  sheet.getRow(temp);//获取第N行表格数据     
		                 if(row != null){
		                	 temp ++ ;
		                	  maganame = row.getCell(1);//获取指定列单元格            
		                	  title = row.getCell(2);   
		                	  titleeng = row.getCell(3);
		                	  author = row.getCell(4);
		                	  yearstr = row.getCell(5);
		                	  periodstr = row.getCell(6);
		                	  pagenum = row.getCell(7);
		                	  pulishaddress = row.getCell(8);
		                	  publishdate = row.getCell(9);
		                	   
		                	  subjectword = row.getCell(10);
		                	  keyword = row.getCell(11);

		                	  abstract_ = row.getCell(12);
		                	  mrcode_ = row.getCell(13); 
				                 if(null != maganame){
				                	 maga.setMaganame(getCell(maganame)); 
				                 }else{
				                	 maga.setMaganame("");
				                 }
				                 if(null != title){
				                	 maga.setTitle(getCell(title)); 
				                 }else{
				                	 maga.setTitle("");
				                 }
				                 if(null != titleeng){
				                	 maga.setEntitle(getCell(titleeng)); 
				                 }else{
				                	 maga.setEntitle("");
				                 }
				                 if(null != author){
				                	 maga.setAuthor(getCell(author)); 
				                 }else{
				                	 maga.setAuthor("");
				                 }
				                 if(null != yearstr){
				                	 maga.setYearstr(getCell(yearstr));
				                 }else{
				                	 maga.setYearstr("");
				                 }
				                 if(null != periodstr){
				                	 maga.setPeriodstr(getCell(periodstr)); 
				                 }else{
				                	 maga.setPeriodstr("");
				                 }
				                 if(null != pagenum){
				                	 maga.setPagestr(getCell(pagenum)); 
				                 }else{
				                	 maga.setPagestr("");
				                 }
				                 
				                 if(null != pulishaddress){
				                	 maga.setPublishaddress(getCell(pulishaddress)); 
				                 }else{
				                	 maga.setPublishaddress("");
				                 } 
				                 if(null != publishdate){
				                	 maga.setPublishdate(getCell(publishdate)); 
				                 }else{
				                	 maga.setPublishdate("");
				                 } 
				                 if(null != subjectword){
				                	 maga.setSubjectword(getCell(subjectword)); 
				                 }else{
				                	 maga.setSubjectword("");
				                 }
				                 if(null != abstract_){
				                	 maga.setAbstract_(getCell(abstract_)); 
				                 }else{
				                	 maga.setAbstract_("");
				                 }
				                 if(null != keyword){
				                	 maga.setKeyword(getCell(keyword)); 
				                 }else{
				                	 maga.setKeyword("");
				                 }
				                 if(null != mrcode_ && StringUtils.isNotEmpty(getCell(mrcode_))){
				                	discode = getCell(mrcode_); 
				                 }else{
				                	 //没有医案编号的记录  不做导入，略过 
				                	  System.out.println("mrcode_:" + getCell(mrcode_));

				                	 continue;
				                 }
				                 
				                 StringBuffer countsql = new StringBuffer();
				                 countsql.append("select id from wdss_resource ");
				                 countsql.append(" where title='");
				                 countsql.append(maga.getBookname());
				                 countsql.append("' and author='");
				                 countsql.append(maga.getAuthor() + "'"); 
								 List accList = DBMgr.getDbUtilInst().queryList(countsql.toString(),null);  
								 int acclen = accList.size();
							     if(acclen == 0){
							    	 maga.setDstype(new Long(2));
							    	 wdssService.saveObject(maga);
							    	 WdssDisinfo disinfo = new WdssDisinfo();
							    	 disinfo.setDiscode(discode);
							    	 disinfo.setDsid(maga.getId());
							    	 disinfo.setState("0");
							    	 wdssService.saveObject(disinfo);
							     }else {
							    	 Map data = (Map)accList.get(0);
								     String id = data.get("id") + "";
//								     WdssResource obj = (WdssResource)wdssService.findByID(WdssResource.class, new Long(id));
//								     title = row.getCell(2);   
//				                	  titleeng = row.getCell(3);
//				                	  author = row.getCell(4);
//				                	  yearstr = row.getCell(5);
//				                	  periodstr = row.getCell(6);
//				                	  pagenum = row.getCell(7);
//				                	  publishdate = row.getCell(8);
//				                	  subjectword = row.getCell(9);
//				                	  keyword = row.getCell(10);
//				                	  abstract_ = row.getCell(11);
							    	 StringBuffer usql = new StringBuffer("update wdss_resource set MAGANAME=?,TITLE=?,ENTITLE=?,AUTHOR=?,SUBJECTWORD=?,KEYWORD=?,ABSTRACT=? ,");
							    	 usql.append("YEARSTR=?,PUBLISHDATE=?,PERIODSTR=?,PAGESTR=? ");
							    	 usql.append(" where id=?");
							    	 Object[] obj = new Object[]{maga.getMaganame(),maga.getTitle(),maga.getEntitle(),maga.getAuthor(),maga.getSubjectword(),maga.getKeyword(),
							    			 maga.getAbstract_(),maga.getYearstr(),maga.getPublishdate(),maga.getPeriodstr(),maga.getPagestr(),id};
							    	 DBMgr.getDbUtilInst().update(usql.toString(), obj);
							     }
		                 }
		                  
		            } 
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
	}
	public String getCell(HSSFCell cell){
		if(cell==null)return "";
		switch(cell.getCellType()){
			case HSSFCell.CELL_TYPE_NUMERIC:
				return (long)cell.getNumericCellValue() + "";
			case HSSFCell.CELL_TYPE_STRING:
				return cell.getStringCellValue();
			case HSSFCell.CELL_TYPE_FORMULA:
				return cell.getCellFormula();
			case HSSFCell.CELL_TYPE_BLANK:
				return "";
			case	HSSFCell.CELL_TYPE_BOOLEAN:
				return cell.getBooleanCellValue() + "";
			case HSSFCell.CELL_TYPE_ERROR:
	    	    return cell.getErrorCellValue() + "";
	    	default:
	    		return "";
		}
	}
	 /**
	  * 数据导出
	  * @param request
	  * @param response
	  * @return
	 * @throws Exception 
	  */
	@RequestMapping("/data/exportData.do")   
	public ModelAndView exportData(HttpServletRequest request,
			HttpServletResponse response) throws Exception { 
		try  {
			String path = this.exportData("");
			System.out.println("message::" + path );
			//  path是指欲下载的文件的路径。 
			response.setCharacterEncoding("UTF-8");
			File file  =   new  File(path);
			String filename  =  file.getName();
			int fileLength = (int)file.length();
			int                 length   = 0;
	        ServletOutputStream op       = response.getOutputStream();
	        ServletContext      context  = request.getSession().getServletContext();
	        String              mimetype = context.getMimeType( filename );
	        byte[]   byteContents   =   new   byte[fileLength];   
	        FileInputStream fis   =   new   FileInputStream(file);   
	        int   retcd   =   fis.read(byteContents);   
	        fis.close();    
	        logger.debug("mimetype:" + mimetype);
	        response.setContentType(mimetype == null?"application/vnd.ms-excel":mimetype );
	        response.setContentLength( retcd );
	        response.setHeader( "Content-Disposition", "attachment; filename=\"" + new String(filename.getBytes("gb2312"),"ISO-8859-1") + "\"" );
	        op.write(byteContents,0,retcd); 
	        op.close();
    }  catch  (IOException ex) {
        ex.printStackTrace();
    }
		return null;  
	} 
	/**
	 * 生成题录数据文件
	 * @return 生成的文件的路径
	 */
	public String exportData(String rid){
		String message = "false";
		 try {
				HSSFWorkbook wb = new HSSFWorkbook(); 
				 HSSFSheet sheet = wb.createSheet("new sheet"); 
				 HSSFRow row = sheet.createRow((short)0); 
				 row.createCell(0).setCellValue("序号"); 
				 row.createCell(1).setCellValue("书名"); 
				 row.createCell(2).setCellValue("著者"); 
				 row.createCell(3).setCellValue("出版社"); 
				 row.createCell(4).setCellValue("出版地"); 
				 row.createCell(5).setCellValue("出版日期"); 
				 row.createCell(6).setCellValue("ISBN号"); 
				 row.createCell(7).setCellValue("版次"); 
				 row.createCell(8).setCellValue("印次"); 
				 row.createCell(9).setCellValue("字数(千字)"); 
				 row.createCell(10).setCellValue("主题词"); 
				 row.createCell(11).setCellValue("关键词"); 
				 row.createCell(12).setCellValue("摘要"); 
				
				 CellStyle cs=wb.createCellStyle(); 
				 List accList = null;
				 if("".equals(rid)){
					 accList = DBMgr.getDbUtilInst().queryList("select * from wdss_resource where dstype=1 order by id",null);
				 }else{
					 accList = DBMgr.getDbUtilInst().queryList("select * from wdss_resource where id=? and dstype=1 order by id",new Object[]{rid});
				 }
				 
				 for(int i=0; i<accList.size(); i++){
						Map data = (Map)accList.get(i);
						HSSFRow onerow = sheet.createRow((short)(i+1)); 
						onerow.createCell(0).setCellValue(String.valueOf(data.get("id"))); 
						onerow.createCell(1).setCellValue((String)data.get("bookname")); 
						onerow.createCell(2).setCellValue((String)data.get("author")); 
						onerow.createCell(3).setCellValue((String)data.get("publishunit")); 
						onerow.createCell(4).setCellValue((String)data.get("publishaddress")); 
						onerow.createCell(5).setCellValue((String)data.get("publishdate")); 
						onerow.createCell(6).setCellValue((String)data.get("isbnstr")); 
						onerow.createCell(7).setCellValue((String)data.get("banci")); 
						onerow.createCell(8).setCellValue((String)data.get("yinci")); 
						onerow.createCell(9).setCellValue((String)data.get("zishu"));
						onerow.createCell(10).setCellValue((String)data.get("subjectword")); 
						onerow.createCell(11).setCellValue((String)data.get("keyword")); 
						onerow.createCell(12).setCellValue((String)data.get("abstract")); 
				 }	
				 String tempFilepath = SystemUtil.getWebappRoot().toString();
				 tempFilepath = tempFilepath.replace("\\", "/") + "/temp";
				 System.out.println("tempFilepath::" + tempFilepath );
				 File tempfile = new File(tempFilepath);
				 if(!tempfile.exists()){
					 tempfile.mkdirs();
				 }
				 String destfile = tempFilepath + "/" + "温病书籍数据.xls";
				 FileOutputStream fileOut = new FileOutputStream(destfile); 
				 wb.write(fileOut); 
				 fileOut.close();
				 message = destfile;
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		return message;
		
	}
}

